package com.luomo.java.lru;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author LiuMei
 * @date 2021-04-21.
 */
public class LRUCacheDemo<k, v>
//        extends LinkedHashMap
{

    class Node<k, v> {
        k key;
        v value;
        Node prev;
        Node next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(k key, v value) {
            this.key = key;
            this.value = value;
        }
    }

    class DoubleLinkedList<k, v> {
        Node<k, v> head;
        Node<k, v> tail;

        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        public void addHead(Node<k, v> node) {
            node.next = this.head.next;
            node.prev = this.head;
            this.head.next.prev = node;
            this.head.next = node;
        }

        public void removeNode(Node<k, v> node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
            node.prev = null;
            node.next = null;
        }

        public Node getLast() {
            if (this.tail.prev == this.head) {
                return null;
            }
            return this.tail.prev;
        }
    }

    private int capacity;
    private Map<k, Node> map;
    private DoubleLinkedList linkedList;


    public LRUCacheDemo(int capacity) {
//        super(capacity, 0.75F, true);
        this.capacity = capacity;
        map = new HashMap<>();
        linkedList = new DoubleLinkedList();
    }

    public v get(k key) {
        Node node = map.get(key);
        if (node == null) {
            return null;
        } else {
            linkedList.removeNode(node);
            linkedList.addHead(node);
            return (v) node.value;
        }
    }

    public void put(k key, v value) {
        if (map.containsKey(key)) {
            Node node = map.get(key);
            node.value = value;
            map.put(key, node);

            linkedList.removeNode(node);
            linkedList.addHead(node);
        } else {
            if (map.size() == capacity) {
                //容量已满，删除最旧的
                Node last = linkedList.getLast();
                linkedList.removeNode(last);
                map.remove(last.key);
            }
            Node node = new Node(key, value);
            map.put(key, node);
            linkedList.addHead(node);
        }
    }


    private Set<k> keySet() {
        return map.keySet();
    }
//    @Override
//    protected boolean removeEldestEntry(Map.Entry eldest) {
//        return size() > capacity;
//    }

    public static void main(String[] args) {
        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);
        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(4, "d");
        System.out.println(lruCacheDemo.keySet());
        System.out.println();
        lruCacheDemo.put(3, "c");
        lruCacheDemo.put(3, "c");
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(5, "e");
        System.out.println(lruCacheDemo.keySet());
    }


}
